package com.example.timecalculous;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.data.DBManger;
import com.example.data.TimeData;
import com.example.diary.Diary;
import com.example.pedometer.SplashActivity;
import com.example.timepicker.ItemDetails;
import com.example.weather.Weather;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.Calendar;

public class MainActivity extends Activity {
    private SlidingMenu slidingMenu;
    public  ListView listview;
    MyAdapter myadapter;
    TimeData timedata;
    DBManger dbManger;
    Cursor mCursor;
    Button btn1,btn2,btn3;
    int TIME_ID=0;
    Calendar calendar=Calendar.getInstance();
    int year,month,day;
    String year1=null,month1=null,day1=null;
    protected void onCreate(Bundle savedInstanceState) {
        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        year1=String.valueOf(year);
        month1=String.valueOf(month);
        day1=String.valueOf(day);
        timedata=new TimeData(this);//初始化数据库
		/*
		 * 初始化数据库中的数据
		 */
        if(isExit()){
            insert(year1,month1,day1,"07", "00", "07","20","起床并洗漱");
            insert(year1,month1,day1,"07", "20", "08", "00", "吃早餐");
        }
        mCursor=select();
        myadapter=new MyAdapter(this,mCursor);
        final AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ImageView imageView1,imageView2;
        imageView1= (ImageView) findViewById(R.id.title_image);
        imageView2= (ImageView) findViewById(R.id.title_add);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               initSlidingMenu();
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert(year1,month1,day1,"12", "30", "12", "30", "具体的安排");
                mCursor.requery();
                listview.setAdapter(myadapter);
                timedata.close();
                myadapter.notifyDataSetChanged();

            }
        });


        listview=(ListView) findViewById(R.id.listview);
        listview.setAdapter(myadapter);
		/*
		 * 设置ListView的点击事件
		 */
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                mCursor.moveToPosition(arg2);
                TIME_ID=mCursor.getInt(0);
                Intent intent=new Intent(MainActivity.this,ItemDetails.class);
                Bundle bundle=new Bundle();
                bundle.putInt("timeid", TIME_ID);
                intent.putExtras(bundle);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        /*
        设置ListView的长按监听事件
         */
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                // TODO Auto-generated method stub
                timedata=new TimeData(MainActivity.this);
                mCursor.moveToPosition(arg2);
                TIME_ID=mCursor.getInt(0);
                dialog.setTitle("删除");
                dialog.setIcon(R.drawable.ic_launcher);
                dialog.setPositiveButton("确定", new  DialogInterface.OnClickListener() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        if(TIME_ID==0){
                            return;
                        }
                        delete(TIME_ID);
                        mCursor.requery();
                        listview.setAdapter(myadapter);
                        timedata.close();
                        myadapter.notifyDataSetChanged();
                    }
                });
                dialog.create();
                dialog.show();
                return true;
            }
        });
        initSlidingMenu();//调用侧滑菜单属性的方法
    }

    /*
     * 设置侧滑菜单的属性
     */
    public void initSlidingMenu(){
        slidingMenu = new SlidingMenu(this);
        slidingMenu.setMode(SlidingMenu.LEFT);
        slidingMenu.setBehindOffset(150);
        slidingMenu.setBehindScrollScale(0);
        slidingMenu.setFadeEnabled(true);
        slidingMenu.setFadeDegree(0.35f);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        slidingMenu.setMenu(R.layout.slidingmenu);
        /*
         * 侧滑菜单子项的点击事件
         */
        btn1=(Button) findViewById(R.id.riji);
        btn2=(Button) findViewById(R.id.tianqiyubao);
        btn3= (Button) findViewById(R.id.jibuqi);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent3=new Intent(MainActivity.this,Diary.class);
                startActivity(intent3);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent4=new Intent(MainActivity.this,Weather.class);
                startActivity(intent4);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent5=new Intent(MainActivity.this,SplashActivity.class);
                startActivity(intent5);
            }
        });
    }

    @Override
    public void onBackPressed() {
        // 点击返回键关闭滑动菜单
        if (slidingMenu.isMenuShowing()) {
            slidingMenu.showContent();
        } else {
            super.onBackPressed();
        }
    }
    /*
     * 判断数据库中是否存在数据
     */
    public boolean isExit(){
        timedata=new TimeData(this);
        mCursor=select();
        boolean result = true;
        if(mCursor.moveToNext()){
            int count=mCursor.getInt(0);
            if(count>0){//存在数据
                result = false;
            }
        }
        return result;
    }
    /*
    数据库——查找方法
     */
    public Cursor select() {
        dbManger=new DBManger();
        SQLiteDatabase db = timedata.getWritableDatabase();
        Cursor cursor=db.query(dbManger.TABLE_NAME,null,null,null,null,null,null);
        return cursor;
    }

    //删除操作
    public void delete(int id)
    {
        dbManger=new DBManger();
        SQLiteDatabase db = timedata.getWritableDatabase();
        String where =dbManger.TIME_ID + " = ?";
        String[] whereValue ={ Integer.toString(id) };
        db.delete(dbManger.TABLE_NAME, where, whereValue);
    }
    //插入操作
    public long insert(String year,String month,String day,String start_hour,String start_minut,String end_hour,
                       String end_minut,String details)
    {
        dbManger=new DBManger();
        SQLiteDatabase db = timedata.getWritableDatabase();
		/* ContentValues */
        ContentValues cv = new ContentValues();
        cv.put(dbManger.YEAR, year);
        cv.put(dbManger.MONTH, month);
        cv.put(dbManger.DAY, day);
        cv.put(dbManger.START_TIME_HOUR, start_hour);
        cv.put(dbManger.START_TIME_MINUT, start_minut);
        cv.put(dbManger.END_TIME_HOUR, end_hour);
        cv.put(dbManger.END_TIME_MINUT, end_minut);
        cv.put(dbManger.ITEM_EDTAILS, details);
        long row = db.insert(dbManger.TABLE_NAME, null, cv);
        return row;
    }
}
